/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.starmeep.classes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author marc
 */
public class StarCluster {
    public static final String CLUSTER_NAME="ClusterStarMeep";
    public static final String CLUSTER_TYPE="starmeep";
    public static final String KNOWN_HOSTS="/root/.ssh/known_hosts";
    

    private boolean running=false;
    private String key_path="";
    private String hostmaster="";
    private ArrayList<StarMeep> jobs=null;

    public StarCluster() {
    }

    public StarCluster(boolean running,String key_path) {
        this.running=running;
        this.key_path=key_path;
        jobs=new ArrayList<StarMeep>();
    }

    public void addJob(StarMeep job){
        jobs.add(job);
    }

    public StarMeep removeJob(int idx){
        StarMeep rmv=jobs.remove(idx);
        return rmv;
    }

    public void givePermissionKey(){
        String command="chmod 600 "+this.getKey_path();
        String res="";
       
            res = StarCommand.runCommand(command);
       
    }

    public static boolean isRunningAlready(){
        String command="/usr/local/bin/starcluster listclusters";
        String res="";
            res = StarCommand.runCommand(command);
        int idx=res.indexOf(CLUSTER_NAME);
        if(idx>0)
            return true;

        return false;
    }

    public String start(){
        String command="/usr/local/bin/starcluster start "+CLUSTER_TYPE+" "+CLUSTER_NAME;
        String res="";
        
            res = StarCommand.runCommand(command);
            System.out.println("La respuesta es "+res);


        if(res.length()>0){
            this.setRunning(true);
        
                res = StarCommand.runCommand("starcluster listclusters");
                int idx=lookIdxType(res, "master", 0);
                this.setHostmaster(getAddress(idx,res));
                
                int just_in_case=0;
                while(idx!=-1&&just_in_case<50){
                    idx=lookIdxType(res, "node", idx+1);
                    StarCommand.runCommand("echo 'data_source \"node"+(just_in_case+1)+"\" "+getAddress(idx,res)+"' >> /etc/ganglia/gmetad.conf");
                    just_in_case++;
                }
                
            //process output
            //test this
        }

        return res;
    }
    private int lookIdxType(String lines,String type,int from){
        return lines.indexOf(type, from);
    }
    private String getAddress(int idx_type,String lines){
        String address="";

        String locate_ini="ec2-";
        String locate_end="amazonaws.com";
        int idx_ini=lines.indexOf(locate_ini, idx_type);
        int idx_end=lines.indexOf(locate_end, idx_ini);

        address=lines.substring(idx_ini, idx_end+locate_end.length());
        System.out.print("Esta es la direccion de los nodos:"+address);

        return address;
    }

    public String stop(){
      //  String command="starcluster stop "+CLUSTER_NAME;
        String command="sh /root/shutdowncluster.sh";
        System.out.println("El comando para stop es "+command);
        String res="";
        
            res = StarCommand.runCommand(command);
        
        if(res.length()>0){
            this.setRunning(false);
            this.setHostmaster("");
        }

        return res;
    }


    /**
     * @return the running
     */
    public boolean isRunning() {
        return running;
    }

    /**
     * @param running the running to set
     */
    public void setRunning(boolean running) {
        this.running = running;
    }

    /**
     * @return the key_path
     */
    public String getKey_path() {
        return key_path;
    }

    /**
     * @param key_path the key_path to set
     */
    public void setKey_path(String key_path) {
        this.key_path = key_path;
    }

    /**
     * @return the hostmaster
     */
    public String getHostmaster() {
        return hostmaster;
    }

    /**
     * @param hostmaster the hostmaster to set
     */
    public void setHostmaster(String hostmaster) {
        this.hostmaster = hostmaster;
    }


}
